package meet_exam.manbang_2023_11_13;


//坑： 1.lamda 表达式不随栈变化，所以引用方法内变量必须是final 对于方法外堆空间变量 可以直接引用
//2. synchronized 有时不能锁this
public class Class01_Async100 {
    static int i = 0;
    static Integer  lock = 0;
    public static void main(String[] args) {

        Thread thread1 = new Thread(() -> {
                while (i < 100) {
                    synchronized (lock){
                        if(i % 2 == 0){
                            System.out.println(Thread.currentThread().getName() +" " + i++);
                        }
                    }
                }
        },"A");
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (i < 100) {
                    synchronized (lock){
                        if(i % 2 == 1){
                            System.out.println(Thread.currentThread().getName() +" " + i++);
                        }
                    }
                }
            }
        },"B");
        thread1.start();
        thread2.start();
    }
}
